/**
 * Created by ptt on 2016/6/7/0007.
 */

define([], function () {
    var matcher = /{{([\s\S]+?)}}|{%([\s\S]+?)%}/g;
    var tpl = function(text) {
        var index = 0;
        var function_body = "with(data || {}){var temp = '';";
        function_body += "temp += '";
        text && text.replace(matcher,function(match, interpolate, evaluate, offset) {
            function_body += text.slice(index,offset);
            if(evaluate){
                function_body += "';" + evaluate + "temp += '";
            }
            if(interpolate){
                function_body += "' + " + interpolate + " + '";
            }
            index = offset + match.length;
            return match;
        });
        function_body += text && text.slice(index);
        function_body += "'};return temp;";
        var render = new Function("data", function_body);
        return {render: render}
    };
    return {
        tpl: tpl
    }
});